//常量  新的关键字const来定义常量，const与let都具有块级作用域：
'use strict';
const pI=999;
console.log(pI);
//解构赋值
var array =['小明','小红','小兰'];
var x=array[0];
var y=array[1];
var z=array[2];

console.log(x,y,z);

var [a,s,d]=['hello','java','es6'];
console.log(a,s,d);

//嵌套
let [q,[w,e]]=['hello',['java','es6']]
console.log(q,w,e);
//忽略某些元素
let [,,p]=['hello','java','es6']
console.log([p]);
//快速获取对象的指定属性
var person ={
    name:'小明',
    age:20,
    gender:'male',
}
var {name,age,gender}=person;
console.log(name+age+gender);

var person ={
    name: '小明',
    age: 20,
    gender: 'male',
    passport: 'G-12345678',
    school: 'No.4 middle school',
    address:{
        city:'beijing',
        street:'no.1 road',
        zipcode:'100001'
    }
};
var {name,address:{city,zip}}=person;
console.log(name);
//name; // '小明'
//city; // 'Beijing'
//zip; // undefined, 因为属性名是zipcode而不是zip
// 注意: address不是变量，而是为了让city和zip获得嵌套的address对象的属性:
//address; // Uncaught ReferenceError: address is not defined

var persosn = {
    name: '小明',
    age: 20,
    gender: 'male',
    passport: 'G-12345678',
    school: 'No.4 middle school'
};


//let {name,passport:id}=persosn;
//console.log(passport);
//解构赋值还可以使用默认值，这样就避免了不存在的属性返回undefined的问题：

var pon={
    name: '小明',
    age: 20,
    gender: 'male',
    passport: 'G-12345678'
};
var {name,single=true}=pon;
console.log(single);//true
//如果变量已经被声明了，再次赋值的时候，正确的写法也会报语法错误
var x,y;
//{x,y} = { name:'小明',x:100,y:200};
//JavaScript引擎把{开头的语句当作了块处理，于是=不再合法
({x,y} = { name:'小明',x:100,y:200});

//交换两个变量x和y的值，可以这么写
var on=1,no=2;
[on,no]=[no,on];
console.log(on,no);
//快速获取当前页面的域名和路径：
var {hostname:domain, pathname:path} = location;
console.log(location);


function buildDate({year, month, day, hour=0, minute=0, second=0}) {
    return new Date(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second);
}

buildDate({ year: 2017, month: 1, day: 1 });